# ==================================
#
#  Code for replicating:
# "Positioning Under Alternative Electoral Systems: Evidence From Japanese Candidate Election Manifestos"
#  Amy Catalinac, NYU
#
# ==================================





# ==================================
# Calculating party-level dispersion 
# (sample: all parties excluding Liberal Alliance and HRP)

load("covars_ideal_points.Rdata") # saved as ideal, sep_theta in 5th column

# Calculate variance in positions within each party and CIs around these variances:
var.by.pty <- by(ideal, list(ideal$pty, ideal$year), function(x) (var(x[,5])))
ci1 <- function(x) (length(x)-1)*
  (var(x)/qchisq((1 - 0.95)/2, (length(x)-1), lower.tail = FALSE))
ci2 <- function(x) (length(x)-1)*
  (var(x)/qchisq((1 - 0.95)/2, (length(x)-1)))
upper.by.pty <- by(ideal, list(ideal$pty, ideal$year), function(x) ci1(x[,5]))
lower.by.pty <- by(ideal, list(ideal$pty, ideal$year), function(x) ci2(x[,5]))
pty.list <- rep(c("ldp", "jsp", "kom", "dsp",  "jcp", "shinsei",  "sakigake", "sdl",  "jnp", "ind",  
                  "ldpi", "jspi",  "komi", "dspi",  "jcpi", "sdli", "jnpi", 
                  "prog", "minor",  "dpj", "nfp",  "lib", "sdp",  "ind2", "dpji",  
                  "nfpi", "dk1",  "dk2", "liba",  "nsp", "musho", "kaikaku", "demref", "cons",
                  "pnp", "nnp", "minna", "kaikakuc", "hrp"),8)
year.list <- c(rep(1986,39), rep(1990,39), rep(1993,39), rep(1996,39), rep(2000,39),rep(2003,39), rep(2005,39), rep(2009,39))
output <- as.data.frame(cbind(
  year.list, 
  pty.list, 
  as.vector(var.by.pty), 
  as.vector(upper.by.pty),
  as.vector(lower.by.pty)))
colnames(output) <- c("year", "pty", "var", "upper.var", "lower.var")
output$year <- as.integer(as.vector(output$year))
output$var <- as.numeric(as.vector(output$var))
output$upper.var <- as.numeric(as.vector(output$upper.var))
output$lower.var <- as.numeric(as.vector(output$lower.var))

# Identify parties to keep:
output.1986 <- output[output$year==1986,]
output.1986.comp <- output.1986[complete.cases(output.1986),]
output.1986.fin <- output.1986.comp[output.1986.comp$pty=="ldp"|
                                      output.1986.comp$pty=="jsp"|
                                      output.1986.comp$pty=="kom"|
                                      output.1986.comp$pty=="dsp"|
                                      output.1986.comp$pty=="jcp" |
                                      output.1986.comp$pty=="sdl"|
                                      output.1986.comp$pty=="shinsei"
                                    ,] #NB: shinsei is actually nls in 1986
output.1990 <- output[output$year==1990,]
output.1990.comp <- output.1990[complete.cases(output.1990),]
output.1990.fin <- output.1990.comp[output.1990.comp$pty=="ldp"|
                                      output.1990.comp$pty=="jsp"|
                                      output.1990.comp$pty=="kom"|
                                      output.1990.comp$pty=="dsp"|
                                      output.1990.comp$pty=="jcp"|
                                      output.1990.comp$pty=="sdl"|
                                      output.1990.comp$pty=="prog"
                                    ,]
output.1993 <- output[output$year==1993,]
output.1993.comp <- output.1993[complete.cases(output.1993),]
output.1993.fin <- output.1993.comp[output.1993.comp$pty=="ldp"|
                                      output.1993.comp$pty=="jsp"|
                                      output.1993.comp$pty=="kom"|
                                      output.1993.comp$pty=="dsp"|
                                      output.1993.comp$pty=="jcp"|
                                      output.1993.comp$pty=="shinsei"|
                                      output.1993.comp$pty=="sakigake"|
                                      output.1993.comp$pty=="jnp"|
                                      output.1993.comp$pty=="sdl"
                                    ,]
output.1996 <- output[output$year==1996,]
output.1996.comp <- output.1996[complete.cases(output.1996),]
output.1996.fin <- output.1996.comp[output.1996.comp$pty=="ldp"|
                                      output.1996.comp$pty=="jcp"|
                                      output.1996.comp$pty=="sakigake"|
                                      output.1996.comp$pty=="dpj"|
                                      output.1996.comp$pty=="nfp"|
                                      output.1996.comp$pty=="sdp"|
                                      output.1996.comp$pty=="nsp"
                                    ,]
output.2000 <- output[output$year==2000,]
output.2000.comp <- output.2000[complete.cases(output.2000),]
output.2000.fin <- output.2000.comp[output.2000.comp$pty=="ldp"|
                                      output.2000.comp$pty=="kom"|
                                      output.2000.comp$pty=="dpj"|
                                      output.2000.comp$pty=="jcp"|
                                      output.2000.comp$pty=="lib"|
                                      output.2000.comp$pty=="sdp"|
                                      output.2000.comp$pty=="cons"
                                    ,]
output.2003 <- output[output$year==2003,]
output.2003.comp <- output.2003[complete.cases(output.2003),]
output.2003.fin <- output.2003.comp[output.2003.comp$pty=="ldp"|
                                      output.2003.comp$pty=="kom"|
                                      output.2003.comp$pty=="dpj"|
                                      output.2003.comp$pty=="jcp"|
                                      output.2003.comp$pty=="sdp"|
                                      output.2003.comp$pty=="cons"
                                    ,]
output.2005 <- output[output$year==2005,]
output.2005.comp <- output.2005[complete.cases(output.2005),]
output.2005.fin <- output.2005.comp[output.2005.comp$pty=="ldp"|
                                      output.2005.comp$pty=="kom"|
                                      output.2005.comp$pty=="dpj"|
                                      output.2005.comp$pty=="jcp"|
                                      output.2005.comp$pty=="sdp"|
                                      output.2005.comp$pty=="pnp"|
                                      output.2005.comp$pty=="nnp"
                                    ,]
output.2009 <- output[output$year==2009,]
output.2009.comp <- output.2009[complete.cases(output.2009),]
output.2009.fin <- output.2009.comp[output.2009.comp$pty=="ldp"|
                                      output.2009.comp$pty=="kom"|
                                      output.2009.comp$pty=="dpj"|
                                      output.2009.comp$pty=="jcp"|
                                      output.2009.comp$pty=="sdp"|
                                      output.2009.comp$pty=="pnp"|
                                      output.2009.comp$pty=="minna"
                                    ,]
data <- rbind(output.1986.fin, 
              output.1990.fin, 
              output.1993.fin, 
              output.1996.fin, 
              output.2000.fin, 
              output.2003.fin,
              output.2005.fin, 
              output.2009.fin)

write.csv(data, file="dispersion_in_parties.csv", row.names=FALSE)
rm(list=ls())





